/**
 * 
 */
package wdmsim;

import java.util.Vector;

import multidomain.Domain;

/**
 * @author alissonslp
 *
 */
public class Flow {

	private long id;
    private int src;
    private int dst;
    private int bw;
    private long duration;
    private int tries;
    private Path path;
    private long requestArrivalTime;
    private Vector<Domain> domainPath;
    private static long nextId=0;

	/**
	 * @param src
	 * @param dst
	 * @param rate
	 * @param holdingTime
	 */
	public Flow(int src, int dst, int rate, long holdingTime, int tries) {
		if (src < 0 || dst < 0 || rate < 1 || holdingTime < 0) {
            throw (new IllegalArgumentException());
        } else {
            this.id = nextId++;
            this.src = src;
            this.dst = dst;
            this.bw = rate;
            this.duration = holdingTime;
            this.tries = tries;
            this.requestArrivalTime = 0;
            this.path = new Path();
            this.domainPath = new Vector<Domain>();
        }
	}

	public int getSrc() {
		return src;
	}

	public void setSrc(int src) {
		this.src = src;
	}

	public int getDst() {
		return dst;
	}

	public void setDst(int dst) {
		this.dst = dst;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getBw() {
		return bw;
	}

	public void setBw(int bw) {
		this.bw = bw;
	}

	public long getDuration() {
		return duration;
	}

	public void setDuration(long duration) {
		this.duration = duration;
	}

	public long getRequestArrivalTime() {
		return requestArrivalTime;
	}

	public void setRequestArrivalTime(long requestArrival) {
		this.requestArrivalTime = requestArrival;
	}

	public int getTries() {
		return tries;
	}

	public void setTries(int tries) {
		this.tries = tries;
	}

	public void increaseTries() {
		this.tries++;
	}
	
	public Path getPath() {
		return path;
	}

	public void setPath(Path path) {
		this.path = path;
	}

	/**
	 * 
	 */
	public void addLightpath(Lightpath lp) {
		this.path.addLightpath(lp);		
	}
	

	public boolean removeLightpath(Lightpath lp) {
		this.path.removeLightpath(lp);
		return false;
	}

	public void addDomain(Domain domain) {
		if(!domainPath.contains(domain))
			domainPath.add(domain);
	}

	public void removeDomain() {
		domainPath.remove(0);		
	}

	public Vector<Domain> getDomainPath() {
		return domainPath;
	}

	/**
	 * @param domainPath2
	 */
	public void setDomainPath(Vector<Domain> domainPath) {
		this.domainPath = domainPath;
		
	}

	/**
	 * @param lp
	 * @return
	 */
	public boolean releaseLightpath(Lightpath lp) {
		int cont = 0;
		Vector<WDMLink> lightpathLinks = getPath().getLightpath(0).getLinks();
		Vector<WDMLink> lpLinks = lp.getLinks();
		try {
			for(int i=0; i<lightpathLinks.size() && cont<lpLinks.size(); i++) {
				if(lightpathLinks.get(i)==lpLinks.get(cont)) {
					if(!lightpathLinks.get(i).release(lp.getWavelength(), this)) {
						throw new Exception();
					}
					cont++;
				}
			}
		} catch(Exception exception) {
			exception.printStackTrace();
			System.exit(0);
		}
		
		return true;
	}
	
}
